package com.xyh.cloudorder.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.Map.Entry;

/**
 * 1.MAP类型数据的key  转换为小写
 * 2.List<Map>类型数据 转json数组
 * @author ykr
 *
 */
public class MapUtil {

	/**
     * 查询出的结果MAP类型数据的key  转换为小写
     */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> convertMapKeyTolowcase(Map<String, Object> paramMap) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Iterator<?> it = paramMap.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, Object> entry = (Entry<String, Object>) it.next();
				String key = (String) entry.getKey();
				resultMap.put(key.toLowerCase(), entry.getValue());
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultMap;
	}
	/**
     * 查询出的结果List集合 MAP类型数据的key  转换为小写
     */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> convertListMapKeyTolowcase(List<Map<String, Object>> paramList) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
		try {
			for(int i = 0;i<paramList.size();i++) {
				Map<String, Object> resultMap = new HashMap<String, Object>();
				Iterator<?> it = paramList.get(i).entrySet().iterator();
				while (it.hasNext()) {
					Entry<String, Object> entry = (Entry<String, Object>) it.next();
					String key = (String) entry.getKey();
					resultMap.put(key.toLowerCase(), entry.getValue());
				}
				resultList.add(resultMap);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}
	/**
	 * Map 转json对象
	 * @param paramMap
	 * @return
	 */
	public static JSONObject mapConvertJsonObject(Map<String, Object> paramMap) {
		try {
			JSONObject jsonObject = (JSONObject) JSON.toJSON(paramMap);
			return jsonObject;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * List<Map>类型数据 转json数组
	 * @param list
	 * @return
	 */
	public static JSONArray listConvertJsonArray(List<Map<String, Object>> list) {
		try {
			JSONArray jsonArray = (JSONArray) JSON.toJSON(list);
			return jsonArray;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
